package shootingtactics.client.core.bullet;

import java.math.BigDecimal;

import shootingtactics.client.core.enemy.BaseEnemy;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;

public class HomingBullet extends BaseBullet{


    static CssColor bulletColor = CssColor.make("rgba(0, 255, 0, 1)");

    public HomingBullet(double posX, double posY, String drawType, Context2d bulletContext, BaseEnemy baseEnemy){
        super(posX, posY, drawType, bulletColor, bulletContext);
        this.targetEnemy = baseEnemy;
        position.setSize(5, 5);
        baseSpeed = 5;
        setInitSpeed();
    }

    public void setInitSpeed(){
        double distanceX = targetEnemy.position.getPositionX() - targetEnemy.getTargetEnemyPositionX();
        double distanceY = targetEnemy.position.getPositionY() - targetEnemy.getTargetEnemyPositionY();
        double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);

        if(distance != 0){
            speedX = distanceX / distance * baseSpeed;
            BigDecimal round =new BigDecimal(speedX);
            speedX = round.setScale(1, BigDecimal.ROUND_UP).doubleValue();

            speedY = distanceY / distance * baseSpeed;
            round = new BigDecimal(speedY);
            speedY = round.setScale(1, BigDecimal.ROUND_UP).doubleValue();
        }else{
            speedX = baseSpeed;
            speedY = 0;
        }
    }

    @Override
    public void updateSpeed(){
        super.updateSpeed();
    }
}
